<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            width: 100%;
            height: 6000px;
            /* background-color: blue; */
        }
        #box1{
            width: 100%;
            height: 80px;
            background-color: #ccc;
        }
        #box2{
            width: 1300px;
            margin: 0 auto;
            height: 80px;
            background-color: #999;
        }
        #souSuoKuang{
            width: 1000px;
            height: 80px;
            background-color: #666;
            margin: 0 auto;
            overflow: hidden;
        }
        #souSuoKuang p{
            width: 300px;
            height: 30px;
            margin: auto;
            border:2px solid coral;
            border-radius: 20px;
            margin-top: 21px;
        }
        #souSuoKuang p span{
            display: block;
            float: right;
            width: 60px;
            height: 30px;
            background-color: darkgrey;
            border-radius: 0 20px 20px 0;
            font-size: 15px;
            text-align: center;
            line-height: 30px;
            color: #fff;
        }
        #btn{
            position: fixed;
            right: 50px;
            bottom: 50px;
        }
    </style>
</head>
<body>
    <div id="box1"></div>
    <div id="box2"></div>
    <div id="souSuoKuang">
        <p>
            <span class="s">搜索</span>
        </p>
    </div>
    <button id="btn">回到顶部</button>
    <script>
        window.onscroll = function () {
            // 判断滚动条的距离 如果大于1000 显示 按钮，小于1000的隐藏按钮
            if (scrollY >= 300) {
                btn.style.display = 'block';
                souSuoKuang.style.position = 'fixed'
                souSuoKuang.style.top = '0'
                souSuoKuang.style.width = '100%'
            } else {
                btn.style.display = 'none'
                souSuoKuang.style.position = 'static'
                souSuoKuang.style.width = '1000px'
                souSuoKuang.style.margin = 'auto'
            }
            var timer
            btn.onclick = function(){
                clearInterval(timer)
                timer = setInterval(function () {

                    if (scrollY <= 0) {
                        clearInterval(timer)
                    }
                    scrollTo({ top: scrollY - 300 })
                }, 100);
            }
        }
    </script>
</body>
</html>